package com.zy.explore.db.infrastructure.plugins.multidbsupport.sqlchange.select;

import com.zy.explore.db.infrastructure.plugins.multidbsupport.sqlchange.base.SqlOper;

import java.util.Collections;
import java.util.List;


public class SqlSelectTest {
	
	public static void main(String[] args) {
		String sql = "";
		// sql += "select\n";
		// sql += "*\n";
		// sql +=",o.organ_name";
		// sql += ",(SELECT group_concat(organ_name) FROM sys_organ o WHERE o.id=s.user_baid) AS baName,\n";
		// sql += "(SELECT\n";
		// sql += "ifnull(organ_name,'-') FROM sys_organ o WHERE o.id=s.user_bapid) AS bapName\n";
		// sql += "from sys_user s\n";
		// sql += "WHERE 1=1\n";
		// sql += "    AND (user_name\n";
		// sql += "    LIKE concat(  '%'  ,  ?  ,  '%'  ) or user_login_name\n";
		// sql += "   LIKE concat('%',concat(1234,'%'))\n";
		//
		// sql += " and sysdata( ) < ?\n";
		// sql += " and to_char('') < ?\n";
		// sql += " and to_char(concat(  '%'  ,  ?  ,  '%'  )) < ?\n";
		// sql += " and max(a.persont) + 0.5 < ?\n";
		// sql += " and max(a.persont + 0.5+(2*3),123) < ?\n";
		// sql += " and date_format(fb.start_time,'%Y-%m-%d') > '2021-01-01'\n";
		// sql += " and DATE_FORMAT \n (am.create_date,\n'%Y-%m-%d') BETWEEN DATE_FORMAT(#{startTime},'%Y-%m-%d') AND DATE_FORMAT(#{endTime},'%Y-%m-%d')\n";
		//
		// sql += " and CAST(a.num_value as DECIMAL) > 10";
		
		//sql = "select * from t_news_message_publish where 1=1 and publish_status ='1'                                             and news_type=?                                  and news_status=?                    order by news_top DESC,top_time DESC,create_date DESC";
		
		// sql = "";
		sql += "     select\n";
		sql += "        series_code seriesCode,year_code yearCode ,\n";
		sql += "        '否' isRepeat,\n";
		sql += "        count(*) items,\n";
		sql += "         \n";
		sql += "        (select max(FOLDER_NO)+1 from t_archives_folder t1 where  t1.series_code=t2.series_code  and t1.year_code=t2.year_code  and del_flag = '0' group by\n";
		sql += "        series_code,year_code)\n";
		sql += "     \n";
		sql += "        startNo\n";
		sql += "        from t_archives_folder t2 where id in\n";
		sql += "         (  \n";
		sql += "            ?\n";
		sql += "         , \n";
		sql += "            ?\n";
		sql += "         ) \n";
		sql += "        and series_code is not null  and year_code is not null \n";
		sql += "         \n";
		sql += "        group by series_code,year_code\n";
		sql += "\n";
		sql += "        union\n";
		sql += "\n";
		sql += "        select\n";
		sql += "        series_code seriesCode,year_code yearCode ,\n";
		sql += "        '否' isRepeat,\n";
		sql += "        count(*) items,\n";
		sql += "         \n";
		sql += "        (select max(FOLDER_NO)+1 from t_archives_folder t1 where  (series_code is null  or series_code='' ) and  (year_code is null  or year_code='' ) group by series_code,year_code)\n";
		sql += "     \n";
		sql += "        startNo\n";
		sql += "        from t_archives_folder t2 where id in\n";
		sql += "         (  \n";
		sql += "            ?\n";
		sql += "         , \n";
		sql += "            ?\n";
		sql += "         ) \n";
		sql += "        and  (series_code is null  or series_code='' ) and  (year_code is null  or year_code='' )\n";
		sql += "         \n";
		sql += "        group by series_code,year_code\n";
		
		System.out.println(sql);
		System.out.println("===============================");
		
		List<SqlOper> sqlOperList = SqlSelectUtils.find(sql);
		
		System.out.println("===============================");
		
		System.out.println(change(sql, sqlOperList));
	}
	

	public static String change(String sql,List<SqlOper> sqlOperList){
		if(sqlOperList.isEmpty()){
			return sql;
		}
		Collections.sort(sqlOperList);
		
		StringBuilder sb = new StringBuilder();
		int pre = 0;
		
		for(SqlOper oper:sqlOperList){
			int starIdx = oper.getStarIdx();
			if(pre < starIdx){
				sb.append(sql, pre, starIdx);
				pre = starIdx;
			}
			
			if(oper.getOper() == SqlOper.OPER_ADD){
				sb.append(oper.getStr());
			}else if(oper.getOper() == SqlOper.OPER_DELETE){
				pre = oper.getEndIdx();
			}else if(oper.getOper() == SqlOper.OPER_REPLACE){
				sb.append(oper.getStr());
				pre = oper.getEndIdx();
			}
		}
		
		if(pre < sql.length()){
			sb.append(sql.substring(pre));
		}
		
		return sb.toString();
		
	}
}
